
#import "M1CustomCardView.h"
#import "M1HomeModel.h"
#import "M1PersonalDataVC.h"
#import "M1PhotoGalleryVC.h"
#import "M1LikePairingVC.h"

@interface M1CustomCardView ()

@property (strong, nonatomic) UIImageView *m1_imageView;

@property (nonatomic, strong) UIView *m1_bgView;

@property (nonatomic, strong) UILabel *m1_name;

@property (nonatomic, strong) UIImageView *m1_vip;

@property (nonatomic, strong) UIImageView *m1_icon;

@property (nonatomic, strong) UILabel *m1_idCard;

@property (nonatomic, strong) UILabel *m1_age;

@property (nonatomic, strong) UILabel *m1_tall;

@property (nonatomic, strong) UILabel *m1_location;

@property (nonatomic, strong) UILabel *m1_school;

@property (nonatomic, strong) M1HomeModel *m1_model;

@end

@implementation M1CustomCardView

- (instancetype)init {
    if (self = [super init]) {
        [self loadComponent];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self loadComponent];
    }
    return self;
}

- (void)m1_loveAction:(UIButton *)sender{
    if (![M1User user]) {
        WEAKSELF
        [M1Alert m1_alertWithTitle:nil message:m1_loginText buttonTitles:@[@"取消", @"登录"] action:^(NSInteger index) {
            if (index == 1) {
                M1LoginVC *loginVC = [[M1LoginVC alloc] init];
                loginVC.m1_changeNavTitleColor = YES;
                loginVC.m1_changeBackImage = YES;
                loginVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.m1_viewController.navigationController pushViewController:loginVC animated:YES];
                
            }
        }];
        return;
    }
    
    
    if ([_m1_loveBtn.imageView.image isEqual:LOAD_LOCAL_IMG(@"home_icon_star")]) {
        [self m1_requestData];
    }
}

- (void) m1_requestData{
    WEAKSELF
    [M1HttpNet requestWithMethod:m1_super_like_url params:@{@"to_uid":self.m1_model.basic.uid} success:^(id resDic, NSInteger code, NSString *message) {
        [weakSelf.m1_loveBtn setImage:LOAD_LOCAL_IMG(@"home_icon_superLike") forState:UIControlStateNormal];
        [M1Alert m1_alertWithTitle:nil message:@"已成功表达心意"];
    } fail:^(id resDic, NSInteger code, NSString *message) {
        if (code == -20001 || code == -20002 || code == -20003) {
            [M1Alert m1_alertWithTitle:nil message:message buttonTitles:@[@"取消", @"立即完善"] action:^(NSInteger index) {
                if (index == 1) {
                    if ([[M1User user].basic.avatar.img.url length] <= 0||[M1User user].basic.photos.count == 0) {
                        M1PhotoGalleryVC *photoVC = [[M1PhotoGalleryVC alloc] init];
                        photoVC.hidesBottomBarWhenPushed = YES;
                        [weakSelf.m1_viewController.navigationController pushViewController:photoVC animated:YES];
                    }else{
                        M1PersonalDataVC *dataVC = [[M1PersonalDataVC alloc] init];
                        dataVC.hidesBottomBarWhenPushed = YES;
                        [weakSelf.m1_viewController.navigationController pushViewController:dataVC animated:YES];
                    }
                }
            }];
        }else if (code >-20000 &&code<=-10000){
            [M1Alert m1_alertWithTitle:nil message:message buttonTitles:@[@"取消", @"前往"] action:^(NSInteger index) {
                if (index == 1) {
                    //会员
                    M1VIPCenterVC *m1_vipVC = [[M1VIPCenterVC alloc]init];
                    m1_vipVC.hidesBottomBarWhenPushed = YES;
                    [weakSelf.m1_viewController.navigationController pushViewController:m1_vipVC animated:YES];
                }
            }];
        }else if (code == -20015){
            [weakSelf.m1_loveBtn setImage:LOAD_LOCAL_IMG(@"home_icon_superLike") forState:UIControlStateNormal];
            M1LikePairingVC *pairingVC = [[M1LikePairingVC alloc] init];
            pairingVC.m1_model = weakSelf.m1_model;
            pairingVC.hidesBottomBarWhenPushed = YES;
            [weakSelf.m1_viewController.navigationController pushViewController:pairingVC animated:YES];
        }else{
            [M1Alert m1_alertWithTitle:nil message:message];
        }
    } showAni:YES];
}

- (void)loadComponent {
    self.backgroundColor = [UIColor colorWithRed:0.951 green:0.951 blue:0.951 alpha:1.00];
    self.m1_imageView = [[UIImageView alloc] init];
    self.m1_imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.m1_imageView.layer setMasksToBounds:YES];
    [self addSubview:self.m1_imageView];
    
    self.m1_imageView.layer.cornerRadius = 10.f;
    self.m1_imageView.layer.masksToBounds = YES;
    
    self.m1_school = [[UILabel alloc] init];
    _m1_school.backgroundColor = [m1_color(0x000000) colorWithAlphaComponent:0.4];
    _m1_school.textColor = m1_color(0xFFFFFF);
    _m1_school.font = m1_font(12);
    _m1_school.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.m1_school];
    
    _m1_school.layer.cornerRadius = 22/ 2;
    _m1_school.layer.masksToBounds = YES;

    self.m1_bgView = [[UIView alloc] init];
    self.m1_bgView.backgroundColor = m1_color(0xffffff);
    [self addSubview:self.m1_bgView];
    self.m1_bgView.layer.cornerRadius = 10.f;
    self.m1_bgView.layer.masksToBounds = YES;
    
    self.m1_name = [[UILabel alloc] init];
    _m1_name.text = @"Cedar";
    _m1_name.textColor = m1_color(0x000000);
    _m1_name.font = m1_font(18);
    [_m1_bgView addSubview:_m1_name];
    
    [_m1_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(27);
        make.left.offset(31);
        make.right.mas_lessThanOrEqualTo(-110);
    }];
    
    self.m1_vip = [[UIImageView alloc] init];
    _m1_vip.image = LOAD_LOCAL_IMG(@"home_icon_vip");
    [_m1_bgView addSubview:_m1_vip];
    
    [_m1_vip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.m1_name.mas_centerY);
        make.left.equalTo(self.m1_name.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(23, 12));
    }];
    
    self.m1_icon = [[UIImageView alloc] init];
    _m1_icon.image = LOAD_LOCAL_IMG(@"home_icon_certification");
    [_m1_bgView addSubview:_m1_icon];
    
    [_m1_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.m1_name.mas_bottom).offset(10);
        make.left.offset(29);
        make.size.mas_equalTo(CGSizeMake(13, 13));
    }];
    
    self.m1_idCard = [[UILabel alloc] init];
    _m1_idCard.text = @"身份已认证";
    _m1_idCard.textColor = m1_color(0x999999);
    _m1_idCard.font = m1_font(12);
    [_m1_bgView addSubview:_m1_idCard];
    [_m1_idCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.m1_icon.mas_centerY);
        make.left.equalTo(self.m1_icon.mas_right).offset(5);
    }];
    
    self.m1_age = [[UILabel alloc] init];
    _m1_age.backgroundColor = m1_color(0xFCF0F0);
    _m1_age.textColor = m1_color(0xFF666B);
    _m1_age.font = m1_font(14);
    _m1_age.textAlignment = NSTextAlignmentCenter;
    [_m1_bgView addSubview:_m1_age];
    
    _m1_age.layer.cornerRadius = 28 / 2;
    _m1_age.layer.masksToBounds = YES;
    
    self.m1_tall = [[UILabel alloc] init];
    _m1_tall.backgroundColor = m1_color(0xFCF0F0);
    _m1_tall.textColor = m1_color(0xFF666B);
    _m1_tall.font = m1_font(14);
    _m1_tall.textAlignment = NSTextAlignmentCenter;
    [_m1_bgView addSubview:_m1_tall];
    
    _m1_tall.layer.cornerRadius = 28 / 2;
    _m1_tall.layer.masksToBounds = YES;
    
    [_m1_tall mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.m1_age.mas_centerY);
        make.left.equalTo(self.m1_age.mas_right).offset(5);
    }];
    
    self.m1_location = [[UILabel alloc] init];
    _m1_location.backgroundColor = m1_color(0xFCF0F0);
    _m1_location.textColor = m1_color(0xFF666B);
    _m1_location.font = m1_font(14);
    _m1_location.textAlignment = NSTextAlignmentCenter;
    [_m1_bgView addSubview:_m1_location];
    
    _m1_location.layer.cornerRadius = 28 / 2;
    _m1_location.layer.masksToBounds = YES;
    
    self.m1_loveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_m1_loveBtn setImage:LOAD_LOCAL_IMG(@"home_icon_star") forState:UIControlStateNormal];
    [_m1_loveBtn addTarget:self action:@selector(m1_loveAction:) forControlEvents:UIControlEventTouchUpInside];
    [_m1_bgView addSubview:_m1_loveBtn];
    
    [_m1_loveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(iPhone5?5:15);
        make.right.offset(-6);
        make.size.mas_equalTo(CGSizeMake(88, 88));
    }];
}

- (void)m1_layoutSubviews {
    
    self.m1_imageView.frame   = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 121);
    
    self.m1_bgView.frame = CGRectMake(0, self.frame.size.height - 131, self.frame.size.width, 131);
}

- (void)installData:(M1HomeModel *)element {
    
    self.m1_model = element;
    
    [self.m1_imageView sd_setImageWithURL:[NSURL URLWithString:element.basic.avatar.img.url] placeholderImage:LOAD_LOCAL_IMG(@"bgView")];
    
    self.m1_name.text = element.basic.name;
    
    if (element.status.member_fees_status == 1) {
        self.m1_vip.hidden = NO;
    }else{
        self.m1_vip.hidden = YES;
    }
    if (element.status.is_me_super == 1) {
        [_m1_loveBtn setImage:LOAD_LOCAL_IMG(@"home_icon_superLike") forState:UIControlStateNormal];
    }else{
        [_m1_loveBtn setImage:LOAD_LOCAL_IMG(@"home_icon_star") forState:UIControlStateNormal];
    }
    
    if ([element.basic.identity_info.identity_auth.auth_status integerValue] == 1) {
        self.m1_icon.hidden = NO;
        self.m1_idCard.hidden = NO;
    }else{
        self.m1_icon.hidden = YES;
        self.m1_idCard.hidden = YES;
    }
    
    if ([element.basic.education_info.school length] > 0) {
        if ([element.basic.education_info.desc length] > 0) {
            self.m1_school.text = [NSString stringWithFormat:@"%@·%@", element.basic.education_info.school, element.basic.education_info.desc];
        }else{
            self.m1_school.text = [NSString stringWithFormat:@"%@", element.basic.education_info.school];
        }
        
        [_m1_school sizeToFit];
        
        [_m1_school mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.m1_bgView.mas_top).offset(-10);
            make.left.offset(20);
            make.size.mas_equalTo(CGSizeMake(self.m1_school.frame.size.width + 20, 22));
            make.right.mas_lessThanOrEqualTo(-40);
        }];
    }
    
    
    self.m1_age.text = [NSString stringWithFormat:@"%@岁", element.basic.age];
    [_m1_age sizeToFit];
    
    [_m1_age mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.m1_idCard.mas_bottom).offset(15);
        make.left.offset(30);
        make.size.mas_equalTo(CGSizeMake(self.m1_age.frame.size.width + 30, 28));
    }];
    
    if ([element.basic.high length] > 0 && [element.basic.high integerValue] != 0) {
        self.m1_tall.text = [NSString stringWithFormat:@"%@cm", element.basic.high];
        
        [_m1_tall sizeToFit];
        
        [_m1_tall mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.m1_age.mas_centerY);
            make.left.equalTo(self.m1_age.mas_right).offset(5);
            make.size.mas_equalTo(CGSizeMake(self.m1_tall.frame.size.width + 30, 28));
        }];
    }
    
    NSArray *abodeArr = [element.basic.abode componentsSeparatedByString:@"-"];
    
    NSString *abode;
    if ([abodeArr count] > 0) {
        if ([[abodeArr firstObject] isEqualToString:@"北京"] || [[abodeArr firstObject] isEqualToString:@"上海"] || [[abodeArr firstObject] isEqualToString:@"天津"] || [[abodeArr firstObject] isEqualToString:@"重庆"]) {
            abode = [abodeArr firstObject];
        }else{
            abode = [abodeArr lastObject];
        }
    }else{
        abode = element.basic.abode;
    }
    
    self.m1_location.text = abode;
    
    [_m1_location sizeToFit];
    
    [_m1_location mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.m1_age.mas_centerY);
        make.left.equalTo(self.m1_tall.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(self.m1_location.frame.size.width + 30, 28));
        make.right.mas_lessThanOrEqualTo(-5);
    }];
    
}

@end
